<template>
	<view class="page">
		<view class="index-top">
			<view class="flexClass">
				<view class="logo">
					<image src="../../static/home/logo.png"></image>
				</view>
				<view class="logo_right uniFlex" @tap="openAddres">
					<view class="provincial" style="color: #fff;">{{provincial}}</view>
					<span class=''></span>
				</view>
			</view>
			<view class="search">
				<view class="search_nf">
					<picker :range="years" value="5"  @change="yearChange">
						{{ years[yearsIndex] }} 年 <span class='r'></span>
					</picker>
				</view>
				<view class="search_in">
					<input class="uni-input" placeholder="请输入项目名称" @confirm="confirm"  v-model="inputText" />
				</view>
				<view class="search_an" @click="goSearch"></view>
			</view>
		</view> 
		<view class="index-cont">
			<view class="">
				<h2>项目进度总体概况</h2>
				<picker :range="months" value="2" @change="monthChange">
					{{ months[monthIndex] }} 月 <span class='r'></span>
				</picker>
				<view class="">
					<view class="cont_xmjd">
						<view class="xmjd_left">
							<image src="../../static/home/xmjd1.png"></image>
							<h2>投资</h2>
						</view>
						<view class="xmjd_right">
							<view>
								<p class="uni-ellipsis">实际完成 <span class='tz'>￥{{zhengshu(homeData.monthCumulativeInvestment)}}亿</span></p>
								<p class='uni-ellipsis r'>年度计划投资 <span>￥{{zhengshu(homeData.planInvestment)}}亿</span></p>
							</view>
							<view class='xmjd_bottom'>
								<progress :percent="homeData.rate" active stroke-width="10" />
							</view>
						</view>
					</view>
					<view class="cont_xmjd m">
						<view class="xmjd_left">
							<image src="../../static/home/xmjd2.png"></image>
							<h2>开工</h2>
						</view>
						<view class="xmjd_right">
							<view>
								<p>实际完成 <span class='kg'>{{homeData.sjkgs}}个</span></p>
								<p class='r'>本年计划开工 <span>{{homeData.jhkgs}}个</span></p>
							</view>
							<view class='xmjd_bottom'>
								<progress :percent="homeData.kgrate" active stroke-width="10" />
							</view>
						</view>
					</view>
					<view class="cont_xmjd l">
						<view class="xmjd_left">
							<image src="../../static/home/xmjd3.png"></image>
							<h2>竣工</h2>
						</view>
						<view class="xmjd_right">
							<view>
								<p>实际完成 <span class='jg'>{{homeData.sjjgs}}个</span></p>
								<p class='r'>本年计划竣工 <span>{{homeData.jhjgs}} 个</span></p>
							</view>
							<view class='xmjd_bottom'>
								<progress :percent="homeData.jgrate" active stroke-width="10" />
							</view>
						</view>
					</view>
				</view>
			</view>
			<view>
				<h2>项目预警情况</h2>
				<view class='cont_nr'>
					<view class="r" @click="toProject('jgyj')">
						<image src="../../static/home/xzyj1.png"></image>
						<view class="cover">
							<span>{{homeData.jgyj}}个</span>
							逾期未竣工项目预警
						</view>
					</view>
					<view class="r" @click="toProject('kgyj')">
						<image src="../../static/home/xzyj1.png"></image>
						<view class="cover">
							<span>{{homeData.kgyj}}个</span>
							逾期未开工项目预警
						</view>
					</view>
					<view class="r" @click="toProject('jdyj')">
						<image src="../../static/home/xzyj1.png"></image>
						<view class="cover">
							<span>{{homeData.jdyj}}个</span>
							投资进度预警
						</view>
					</view>
				</view>
			</view>
			<view>
				<h2>项目动态</h2>
				<view class='cont_xm'>
					<view class='cont_xm_nr r'>
						<p @click="toProject('zhongdPro')"><span>{{homeData.xmgs}}个</span>{{zdxmTitle}} </p>
						<p @click="toProject('ldxm')" v-if="dynamic"><span>{{homeData.ldxmgs}}个</span>省领导联系项目</p>
						<p @click="toProject('czt')" v-if="dynamic"><span>{{homeData.cztxmgs}}个</span>长株潭标志工程</p>
					<!-- </view> -->
					<!-- <view class='cont_xm_nr'> -->
						<p @click="toProject('jcss')" v-if="showQuanxian"><span>{{homeData.jcssxmgs}}个</span>十大基础设施项目</p>
						<p @click="toProject('szd')" v-if="showQuanxian"><span>{{homeData.cyxmgs}}个</span>十大产业项目</p>
						<p @click="goXmjx" v-if="showQuanxian"><span><image src="../../static/home/sy_bj13.png"></image></span>进度汇总</p>
					<!-- </view>
					<view class='cont_xm_nr'> -->
					</view>
				</view>
			</view>
			<view v-show="false">
				<h2>日常办公</h2>
				<view class='cont_nr r'>
					<!-- <view class="a">
						<navigator url="../month/index">
							<image src='../../static/home/rcbg1.png'></image>
							<p>月报填报</p>
						</navigator>
					</view>
					<view class="a">
						<navigator url="../month/shenhe">
							<image src='../../static/home/rcbg2.png'></image>
							<p>月报审核</p>
						</navigator>
					</view> -->
					<view class="a">
						<navigator url="../zdwt/index">
							<image src='../../static/home/rcbg3.png'></image>
							<p>问题管理</p>
						</navigator>
					</view>
					<view class="a">
						<navigator url="../zdwt/index">
							<image src='../../static/home/rcbg3.png'></image>
							<p>问题查看</p>
						</navigator>
					</view>
				</view>
			</view>
			<view class="bottom">
			</view>
		</view>
		
	</view>
</template>

<script>
	import simpleAddress from '@/pages/component/simple-address/simple-address.vue';
	export default {
		data() {
			return {
				inputText:"",
				years:[],
				yearsIndex:0,
				year:"",
				months:["1","2","3","4","5","6","7","8","9","10","11","12"],
				month:"",
				monthIndex:0,
				provincial:"省本级",
				xmyjqk:[
					{
						index:'1',
						num:'0个',
						pro:'逾期竣工项目预警'
					},
					{
						index:'2',
						num:'0个',
						pro:'逾期开工项目预警'
					},
					{
						index:'3',
						num:'0个',
						pro:'投资进度预警'
					},
				],
				//titleArr:['省本级','市州级','县市区级'],
				// shizhouArr:[],
				// quxianArr:[],
				pickIsShow:false,
				//cityPickerValueDefault: [0, 0, 1],
				pickerText: '',
				//regionIndex:[0,0,0],
				column:0,
				homeData:{},
				rcbg:[
					{
						index:'1',
						name:'月报填报',
						path:'/pages/month/index',
					},
					{
						index:'2',
						name:'月报审核',
						path:'/pages/month/shenhe',
					},
					{
						index:'3',
						name:'问题管理',
						path:'/pages/zdwt/index',
					},
				],
				roleType:"",
				zdxmTitle:"省重点建设项目",
				dynamic:true,
				showQuanxian:true,
				region:"",
				cityid:"",
				countryid:""
			}
		},
		onLoad(options) {
			this.versionUpdate();
			console.log(options);
			var that = this;
			//获取存储的角色
			this.roleType= uni.getStorageSync("roleType");
			console.log(this.roleType);
			if(this.roleType==1||this.roleType==2||this.roleType==7){
				this.showQuanxian = true
			}else{
				this.showQuanxian=false
			}
			this.getYears();
			this.loading()
			//当前年份
			this.$http.get("/sys/v1/currentYear.do",{
				
			}).then(res=>{
				//console.log(res)
				this.year= res.data;
				let yearsIndex=0;
				yearsIndex = this.years.indexOf(this.year);
				this.yearsIndex = yearsIndex;
			})
			//当前月份
			this.$http.get("/sys/v1/currentMonth.do",{
				
			}).then(res=>{
				console.log(res);
				this.month = res.data;
				let monthIndex=0;
				monthIndex = this.months.indexOf(this.month);
				this.monthIndex = monthIndex;
			})
		},
		
		
		methods: {
			zhengshu(num){
				return parseInt(num);
			},
			//年份取后台
			getYears(){
				this.$http.get("/sys/v1/dict.do?code=year",{
					
				}).then(res=>{
					console.log(res);
					let years = res.data;
					for(var i=0;i<years.length;i++){
						let yearName = years[i].name;
						this.years.push(yearName);
					}
					console.log(this.years)
				})
			},
			
			cityChange(e){
				let changePickerValue = e.detail.value;
			},
			upData(backData){
				 console.log(backData)
				this.provincial = backData.contentText;
				var level = backData.level;
				this.dynamic = true;
				this.showQuanxian = true;
				if(level=="省级"){
					this.zdxmTitle="省重点建设项目"
					this.dynamic = true;
					this.region = 1;
					this.loading();
				}else if(level=="市级"){
					this.zdxmTitle="市重点建设项目";
					this.dynamic = false;
					this.showQuanxian = false;
					this.region = 2;
					this.cityid = backData.cityid;
					uni.showLoading({
						title:"加载中"
					})
					this.loading();
					setTimeout(()=>{
						uni.hideLoading()
					},2000)
				}else if(level=="县级"){
					this.zdxmTitle="县重点建设项目";
					this.dynamic = false;
					this.showQuanxian = false;
					this.region = 3;
					this.cityid = backData.cityid;
					this.countryid = backData.countryid;
					this.loading()
				}
			},
			openAddres() {
				uni.navigateTo({
					url: "../city/city"
				})
			},
			onConfirm(e) {
				this.pickerText = JSON.stringify(e);
			},
			//搜索框搜索按钮
			confirm(){
				this.goSearch();
				uni.hideKeyboard();
			},
			goSearch(){
				let projectName= this.inputText;
				console.log(projectName);
				uni.navigateTo({
					url:"../xmjz/index?projectName="+projectName
				})
			},
			//选择年份
			yearChange:function(e){
				this.yearsIndex = e.detail.value;
				uni.showLoading({
					title:"加载中……"
				})
				this.loading();
				setTimeout(function () {
				    uni.hideLoading();
				}, 2000);
			},
			//选择月份
			monthChange(e){
				this.monthIndex = e.detail.value;
				uni.showLoading({
					title:"加载中……"
				})
				this.loading();
				setTimeout(function () {
				    uni.hideLoading();
				}, 2000);
			},
			loading:function(){
				let years = this.years;
				let yearsIndex = this.yearsIndex;
				let year = years[yearsIndex];
				let months = this.months;
				let monthIndex = this.monthIndex;
				let month = months[monthIndex];
				console.log(year);
				this.$http.get("/project/v1/syInfo.do",{
						 params:{
							 year:year,
							 month:month,
							 region:this.region,
							 cityid:this.cityid,
							 countryid:this.countryid
							}
					}).then(data=>{
						console.log(data);
						this.homeData = data.data;
						console.log(this.homeData);
					}).catch(e=>{
							console.log("请求错误",e)
						});
			},
			toUserPro(){
				uni.switchTab({
					url:"/pages/project/userProject"
				})
			},
			toProject(conditions){
				console.log(conditions);
				if(conditions=='czt'){
					uni.navigateTo({
						url:"/pages/xmjz/cztlist"
					})
				}else if(conditions=='jcss'){
					uni.navigateTo({
						url:"/pages/xmjz/jcsslist"
					})
				}else if(conditions=='szd'){
					uni.navigateTo({
						url:"/pages/xmjz/shidachanyeProj"
					})
				}else if(conditions=='ldxm'){
					uni.navigateTo({
						url:"/pages/xmjz/ldxm"
					})
				} 
				else{
					uni.navigateTo({
						url:"/pages/xmjz/index?conditions="+conditions+"&region="+this.region+"&cityid="+this.cityid+"&countryid="+this.countryid
					 })
				}
				
				
			},
			goXmjx(){
				uni.navigateTo({
					url:'/pages/xmjz/tongji'
				})
			},
			
						//版本检测
							versionUpdate(){
								//#ifdef APP-PLUS
								// APP检测更新 具体打包流程可以参考：https://ask.dcloud.net.cn/article/35667
								plus.screen.lockOrientation('portrait-primary'); //竖屏正方向锁定
								//获取是否热更新过
								const updated = uni.getStorageSync('updated'); // 尝试读取storage
								
								if (updated.completed === true) {
									// 如果上次刚更新过
									// 删除安装包及安装记录
									console.log('安装记录被删除，更新成功');
									uni.removeSavedFile({
										filePath: updated.packgePath,
										success: res => {
											uni.removeStorageSync('updated');
										}
									});
								} else if (updated.completed === false) {
									uni.removeStorageSync('updated');
									plus.runtime.install(updated.packgePath, {
										force: true
									});
									uni.setStorage({
										key: 'updated',
										data: {
											completed: true,
											packgePath: updated.packgePath
										},
										success: res => {
											console.log('成功安装上次的更新，应用需要重启才能继续完成');
										}
									});
									uni.showModal({
										title: '提示',
										content: '应用将重启以完成更新',
										showCancel: false,
										complete: () => {
											plus.runtime.restart();
										}
									});
								} else {
									console
										.log(
											'成功保存记录'
										);
									//获取当前系统版本信息
									plus.runtime.getProperty(plus.runtime.appid, widgetInfo => {
										//请求后台接口 解析数据 对比版本
										
										this.$http.get('/sys/v1/version.do?id=1').then(res => {
											res = res.data
 										//	uni.showLoading({title:widgetInfo.version});
										//	uni.showLoading({title: res.version});
											if ((res.android_wgt_url || res.ios_wgt_url) && widgetInfo.version < res.version) {
												let downloadLink = '';
												let androidLink = res.android_wgt_url;
												console.log(downloadLink);
												let iosLink = res.ios_wgt_url;
												let ready = false;
												//校验是是不是热更新
												if (res.android_wgt_url.match(RegExp(/.wgt/))) {
													// 判断系统类型
													if (plus.os.name.toLowerCase() === 'android') {
														console.log('安卓系统');
														if (androidLink) {
															// 我这里默认#也是没有地址，请根据业务自行修改
															console.log('发现下载地址');
															// 安卓：创建下载任务
															if (androidLink.match(RegExp(/.wgt/))) {
																console.log('确认wgt热更新包');
																downloadLink = androidLink;
																ready = true;
															} else {
																console.log(
																	'安卓推荐.wgt强制更新，.apk的强制更新请您自行修改程序'
																);
															}
														} else {
															console.log('下载地址是空的，无法继续');
														}
													} else {
														console.log('苹果系统');
														if (iosLink) {
															// 我这里默认#也是没有地址，请根据业务自行修改
															console.log('发现下载地址');
															// 苹果(A)：进行热更新（如果iosLink是wgt更新包的下载地址）判断文件名中是否含有.wgt
															if (iosLink.match(RegExp(/.wgt/))) {
																console.log('确认wgt热更新包');
																downloadLink = iosLink;
																ready = true;
															} else {
																console.log('苹果只支持.wgt强制更新');
															}
														} else {
															console.log('下载地址是空的，无法继续');
														}
													}
													if (ready) {
														console.log('任务开始');
														let downloadTask = uni.downloadFile({
															url: downloadLink,
															success: res => {
																if (res.statusCode ===
																	200) {
																	// 保存下载的安装包
																	console.log('保存安装包');
																	uni.saveFile({
																		tempFilePath: res
																			.tempFilePath,
																		success: res => {
																			const
																				packgePath =
																				res
																				.savedFilePath;
																			// 保存更新记录到stroage，下次启动app时安装更新
																			uni.setStorage({
																				key: 'updated',
																				data: {
																					completed: false,
																					packgePath: packgePath
																				},
																				success: () => {
																					console
																						.log(
																							'成功保存记录'
																						);
																				}
																			});
																			// 任务完成，关闭下载任务
																			console
																				.log(
																					'任务完成，关闭下载任务，下一次启动应用时将安装更新'
																				);
																			downloadTask
																				.abort();
																			downloadTask
																				=
																				null;
																		}
																	});
																}
															}
														});
													} else {
														console.log('下载地址未准备，无法开启下载任务');
													}
												} else {
													//不是热更新是在线更新 校验是否强制升级
													if (res.method == 'true') {
														uni.showModal({
															showCancel: false,
															confirmText: '立即更新',
															title: '发现新版本',
															content: res.DESCRIPTION,
															success: res => {
																if (res.confirm) {
																	console.log("确定");
																	downloadLink;
																	uni.showLoading
																		.showLoading(
																			'下载中...');
																	if (uni
																		.getSystemInfoSync()
																		.platform ==
																		'android') {
																		uni.downloadFile({
																			url: androidLink,
																			success: downloadResult => {
																				if (downloadResult
																					.statusCode ===
																					200
																				) {
																					plus.runtime
																						.install(
																							downloadResult
																							.tempFilePath, {
																								force: false
																							},
																							d => {
																								console
																									.log(
																										'install success...'
																									);
																								plus.runtime
																									.restart();
																							},
																							e => {
																								console
																									.error(
																										'install fail...'
																									);
																							}
																						);
																				}
																			}
																		});
																	}
																	if (uni
																		.getSystemInfoSync()
																		.platform == 'ios'
																	) {
																		plus.runtime
																			.openURL(
																				iosLink,
																				function(
																					res) {}
																			);
																	}
																} else if (res.cancel) {
																	console.log('取消');
																}
															}
														});
													} else {
														uni.showModal({
															title: '发现新版本',
															confirmText: '立即更新',
															cancelText: '下次更新',
															content: res.DESCRIPTION,
															success: res => {
																if (res.confirm) {
																	uni.showLoading({
																		title:"下载中"
																	})
																	if (uni.getSystemInfoSync().platform =='android') {
																		
																		uni.downloadFile({
																			url: androidLink,
																			success: downloadResult => {
																				if (downloadResult
																					.statusCode ===
																					200
																				) {
																					plus.runtime
																						.install(
																							downloadResult
																							.tempFilePath, {
																								force: false
																							},
																							d => {
																								console
																									.log(
																										'install success...'
																									);
																								plus.runtime
																									.restart();
																							},
																							e => {
																								console
																									.error(
																										'install fail...'
																									);
																							}
																						);
																				}
																			}
																		});
																	}
																	if (uni
																		.getSystemInfoSync()
																		.platform == 'ios'
																	) {
																		plus.runtime
																			.openURL(
																				iosLink,
																				function(
																					res) {}
																			);
																	}
																} else if (res.cancel) {
																	console.log('取消');
																}
															}
														});
													}
												}
											}
										});
									});
								}
								
								//#endif
			}
			
			
		}
	}
</script>

<style scoped>
	@import url("index.css");
	page{
		font-family: 'my-font';
	}
	.mask{
		width: 100%;
		height: 100%;
		background-color: #000000;
		opacity: 0.2;
		z-index: 100;
		position: absolute;
		top: 0;
		left: 0;
	}
	>>>.uni-progress-bar {  
	        border-radius: 10px;  
	    }  
	>>>.uni-progress-inner-bar {  
		border-radius: 5px;	
		background: linear-gradient(to right,#FAD0C4,#EA6D72) !important;
	}
	/deep/.cont_xmjd.m .uni-progress-inner-bar{
		background: linear-gradient(to right,#C4E3FA,#6DB5EA) !important;
	}
	/deep/.cont_xmjd.l .uni-progress-inner-bar{
		background: linear-gradient(to right,#FAEBC4,#EAB56D) !important;
	}
</style>
